import { questionDetail } from "../../../api/ask"
import { shareAppMessageSet } from "../../../utils/config"
import { dataCollection } from "../../../utils/dataCollection"
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    total: 0,
    questionId: '',
    list: <any[]>[],
    userAvatar: '',
    userPhone: '',
    userQuestion: '',
    themeCode: '',
    hint:{
      backgroundColor: '#FEEAEA',
      icon: 'http://static.pub-drive.chexinlian.com/wxmini/43b956fccb012ad0'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    //从本地获取当前选择的城市编码
    const themeCode = wx.getStorageSync('themeCode')
    const question = wx.getStorageSync('question')
    this.setData({
      questionId: options.id,
      userAvatar: question.userAvatar || '',
      userPhone: question.phone || '',
      userQuestion: question.content || '',
      themeCode,
      hint: app.globalData.dictMap.get('answerHint') 
    })
    this.getAnswerList()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  bindscrolltolower() {
    if (this.data.list.length < Number(this.data.total)) {
      this.getAnswerList()
    }
  },
  //获取回答的数据
  getAnswerList() {
    questionDetail(this.data.current, this.data.questionId).then(res => {
      let i = this.data.current
      i++
      const list: any[] = this.data.list
      if (this.data.current === 1) {
        list.length = 0
      }
      list.push(...res.result.records)

      this.setData({
        current: i,
        total: res.result.total,
        list: [...res.result.records]
      })
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return { ...shareAppMessageSet() }
  },
  onHide() {
    dataCollection.setPageMapData('leave')
  },
  onUnload() {
    dataCollection.setPageMapData('leave')
  },
  onShow() {
    dataCollection.setPageMapData('enter', '全部问答')
  },
  onPreviewImage(res: any) {
    const index = res.currentTarget.dataset.index
    const i = res.currentTarget.dataset.i
    const urls: string[] = [];
    this.data.list[index].contentPictures.forEach((item: any) => {
      urls.push(item);
    });
    wx.previewImage({
      urls,
      showmenu: true,
      current: this.data.list[index].contentPictures[i],
      fail: (e) => {
        console.log(e);
      },
    });
  },
})